package com.liming.cartype.webapp.wicket.page;

import com.liming.cartype.api.biz.IBrandService;
import com.liming.cartype.api.biz.IProductLineService;
import com.liming.cartype.api.persistence.impl.BrandBeanImpl;
import com.liming.cartype.api.persistence.impl.ProductLineBeanImpl;
import com.liming.core.web.wicket.BasePage;
import org.apache.aries.blueprint.annotation.Reference;
import org.apache.wicket.AttributeModifier;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.form.AjaxFormValidatingBehavior;
import org.apache.wicket.ajax.markup.html.AjaxLink;
import org.apache.wicket.ajax.markup.html.form.AjaxButton;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.IChoiceRenderer;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.repeater.Item;
import org.apache.wicket.markup.repeater.data.DataView;
import org.apache.wicket.markup.repeater.data.ListDataProvider;
import org.apache.wicket.model.*;
import org.apache.wicket.util.time.Duration;

import javax.inject.Inject;
import javax.inject.Named;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @类描述：产品页面
 * @创建人：sunlf
 * @创建时间：2014-03-29 下午12:00
 * @修改人：
 * @修改时间：
 * @修改备注：
 */

public class ProductLinePage extends BasePage {
    @Named
    @Inject
    @Reference(id = "productLineService", serviceInterface = IProductLineService.class)
    private IProductLineService productLineService;

    @Named
    @Inject
    @Reference(id = "brandService", serviceInterface = IBrandService.class)
    private IBrandService brandService;


    Label label1 = new Label("pageName1", "添加产品");
    Label label2 = new Label("pageName2", "编辑产品");
    final DataView<ProductLineBeanImpl> productLines;

    public ProductLinePage() {
        label1.setVisible(true);
        label2.setVisible(false);
        add(label1);
        add(label2);

        final Form<ProductLineBeanImpl> form = new Form<ProductLineBeanImpl>("add_productLine_form", new CompoundPropertyModel<ProductLineBeanImpl>(new ProductLineBeanImpl()));
        add(form);
        AjaxFormValidatingBehavior.addToAllFormComponents(form, "keydown", Duration.ONE_SECOND);

        TextField formName = new TextField("name");

        TextField formSummary = new TextField("theme");
        final  List<BrandBeanImpl> oneLevelKindList = brandService.getAllBrand();
        final DropDownChoice oneLevelKind = new DropDownChoice("parentId",new Model(),oneLevelKindList);
        oneLevelKind.setChoiceRenderer(new IChoiceRenderer() {
            public Object getDisplayValue(Object o) {     //设置displayName
                BrandBeanImpl k = (BrandBeanImpl) o;
                return k.getName();
            }
            public String getIdValue(Object o, int arg1) {    //设置Value
                BrandBeanImpl k = (BrandBeanImpl) o;
                return String.valueOf(k.getId());
            }
        });
        form.add(formName);

        form.add(formSummary);
        form.add(oneLevelKind);
        form.add(new AjaxButton("submit_button", form) {
            @Override
            protected void onSubmit(AjaxRequestTarget target, Form<?> submitForm) {
                String id = oneLevelKind.getModelValue();
                BrandBeanImpl modelObject = (BrandBeanImpl)oneLevelKind.getModelObject();
                ProductLineBeanImpl productLineBean = (ProductLineBeanImpl) getParent().getDefaultModelObject();
                productLineBean.setCreateDate(new Date());
                productLineBean.setState("未发布");
                productLineBean.setParentId(modelObject.getId());
                ProductLineBeanImpl productLineBean1 = productLineService.putProductLine(productLineBean);
                BrandBeanImpl brandBean = brandService.findBrand(id);
                if(null != brandBean){
                    List<String> lineBeanList = brandBean.getProductLines();
                    if(null != lineBeanList){
                        lineBeanList.add(productLineBean1.getId());
                    }else{
                        lineBeanList = new ArrayList<String>();
                        lineBeanList.add(productLineBean1.getId());
                    }
                    brandBean.setProductLines(lineBeanList);
                    brandService.putBrand(brandBean);
                }
                setResponsePage(ProductLinePage.class);
                label1.setVisible(true);
                label2.setVisible(false);
            }

            @Override
            protected void onError(AjaxRequestTarget target, Form<?> form) {
                System.out.println("onError");
            }
        });

        add(new Link("reset_form_1") {
            @Override
            public void onClick() {
                form.setModelObject(new ProductLineBeanImpl());
                label1.setVisible(true);
                label2.setVisible(false);
            }
        });

        form.add(new Link("reset_form_2") {
            @Override
            public void onClick() {
                form.setModelObject(new ProductLineBeanImpl());
                label1.setVisible(true);
                label2.setVisible(false);
            }
        });



        //添加数据列表
        List<ProductLineBeanImpl> distributorList = productLineService.getAllProductLine();
        ListDataProvider<ProductLineBeanImpl> listDataProvider = new ListDataProvider<ProductLineBeanImpl>(distributorList);
        productLines = new DataView<ProductLineBeanImpl>("productLineRow", listDataProvider) {
            private static final long serialVersionUID = 1L;

            @Override
            protected void populateItem(final Item<ProductLineBeanImpl> item) {
                final ProductLineBeanImpl distributor;
                distributor = item.getModelObject();

                item.add(new Label("name", distributor.getName()));
                BrandBeanImpl brandBean = brandService.findBrand(distributor.getParentId());
                if(null != brandBean){
                    item.add(new Label("parentId", brandBean.getName()));
                }else{
                    item.add(new Label("parentId",""));
                }

                item.add(new Label("theme", distributor.getTheme()));
                item.add(new Label("createDate", distributor.getCreateDate()));
                item.add(new Label("number", distributor.getNumber()));
                item.add(new Label("state", distributor.getState()));

                item.add(new AjaxLink("edit") {
                    @Override
                    public void onClick(AjaxRequestTarget target) {
                    }
                });
                item.add(new Link("del") {
                    @Override
                    public void onClick() {
                    }
                });
                item.add(AttributeModifier.replace("class", new AbstractReadOnlyModel<String>() {
                    private static final long serialVersionUID = 1L;

                    @Override
                    public String getObject() {
                        return (item.getIndex() % 2 == 1) ? "even" : "odd";
                    }
                }));
            }
        };
        productLines.setOutputMarkupId(true);
        add(productLines);
    }
}